﻿@model IndignadoFramework.FrontOffice.Models.LogOnModel

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

<!-- Javascript para Login de Facebook -->
<div id="script_root"></div>
<script type="text/javascript">
    window.fbAsyncInit = function () {
        FB.init({ appId: '279088262186683',
            status: true,
            cookie: true,
            xfbml: true,
            oauth: true
        });

        function updateButton(response) {
            // Campos del form a ser enviados
            var id = document.getElementById('Id');
            var redSocial = document.getElementById('RedSocial');

            // Boton de Sign in Facebook
            var facebookButton = document.getElementById('fb_auth');

            // Link de Sign out
            var signout = document.getElementById('logout');

            if (response.authResponse) {
                // Usuario logueado y conectado
                FB.api('/me',
					function (response) {
					    // Seteo variables del form
					    if (id != null) {
					        id.value = response.id;
					        redSocial.value = "F";
					        // Submit del form
					        if (response.id != "undefined") {
					            document.forms['reg_form'].submit();
					        }
					    }
					}
				);
            }
            if (facebookButton != null) {
                facebookButton.onclick =
					function () {
					    if (id.value != "" && id.value != "undefined" && redSocial.value != "") {
					        document.forms['reg_form'].submit();
					    }
					    else {
					        FB.login(
							    function (response) {
							        if (response.authResponse) {
							            FB.api('/me',
										    function (response) {
										        // Seteo variables del form
										        id.value = response.id;
										        redSocial.value = "F";
										        // Submit del form
										        if (response.id != "undefined") {
										            document.forms['reg_form'].submit();
										        }
										    }
									    ); // FB.api
							        } else {
							            // El usuario cancelo el login o no autorizo las credenciales
							        }
							    }, { scope: 'email,user_location'} // scope refiere a los datos que traigo de Facebook
						    ); // FB.login
					    }
					}
            }

            if (signout != null) {
                signout.onclick =
					function () {
					    // Submit del form
					    document.forms['logout_form'].submit();
					}
            }
        }

        // run once with current status and whenever the status changes
        FB.getLoginStatus(updateButton);
        FB.Event.subscribe('auth.statusChange', updateButton);
    };

	(function () {
		var id, e = 'facebook-jssdk'; e = document.createElement('script');
		e.id = id; e.async = true; e.src = document.location.protocol
						+ '//connect.facebook.net/es_LA/all.js';
		document.getElementById('script_root').appendChild(e);
	} ());
</script>

@if (Request.IsAuthenticated)
{
    using (Html.BeginForm("LogOff", "Account", FormMethod.Post, new { id = "logout_form" }))
    {
	<text>Bienvenido <strong>@ViewBag.UsuarioNombre</strong>!</text>
	<a href="#" id="logout">Salir</a>
    }
    @Html.ActionLink("Conéctate para chatear", "VentanaChat", "Chat");
}
else
{
    <div id="social_buttons">
		<div id="fb_auth" class="facebookButton"></div>
		<div id="tw_auth" class="twitterButton"></div>
    </div>
	
    // Javascript para Login de Twitter
	<script src="http://platform.twitter.com/anywhere.js?id=ouDit0LgNM0zNIFJC9nHnw&v=1" type="text/javascript"></script>
	<script type="text/javascript">
		// Boton de Sign in Twitter
		var twitterButton = document.getElementById('tw_auth');

		if (twitterButton != null) {
			twttr.anywhere(function (T) {
				twitterButton.onclick = function () {
					T.signIn();
				};
			});
		}
	</script>
	
    using (Html.BeginForm("LogOn", "Account", FormMethod.Post, new { id = "reg_form" }))
    {
    // Campos ocultos que componen el form
	<div style="display: none">
		<div class="editor-field">
			@Html.TextBoxFor(m => m.Id)
			@Html.ValidationMessageFor(m => m.Id)
		</div>

		<div class="editor-field">
			@Html.TextBoxFor(m => m.RedSocial)
			@Html.ValidationMessageFor(m => m.RedSocial)
		</div>
	</div>
	}
}